package com.eagle.coders.security.beans;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.eagle.coders.common.bean.annotations.DomainTitle;
import com.eagle.coders.common.bean.annotations.PropertyConstraints;
import com.eagle.coders.common.bean.annotations.UiComponentPosition;
import com.eagle.coders.common.bean.annotations.UnBound;
import com.eagle.coders.common.bean.annotations.type.MandatoryType;
import com.eagle.coders.common.bean.annotations.type.StringInputCaseType;
import com.eagle.coders.common.bean.annotations.type.TextFiledInputDataType;



/**
 * @author Anees
 *
 */
@Entity
@Table(name ="eagle_permission")
@DomainTitle(title = "User Permission")
@SequenceGenerator(name = "permission_gen_seq", sequenceName = "eagle_gen_seq")
public class PermissionsBean implements Serializable {
	
	/**
	 * 
	 * @author Anees
	 *
	 */
	public enum PermissionType{
		EXECUTE("1"), WRITE("2"),WRITE_EXECUTE("3"), READ("4"), READ_EXECUTE("5"), READ_WRITE("6") ,ALL("7") ;
		
		private String string;
		
		private PermissionType(String string){
			this.string = string;
		}
		
		public String getValue(){
			return this.string;
		}
	}
	
	/**
	 * 
	 * @author Anees
	 *
	 */
	public enum PermissionAccessType {
		
		DENIED, ALLOWED
	}

	private static final long serialVersionUID = 1L;

	private Integer id;
	private List<ActionBean> actionID;
	private PermissionType permissionType;
	
	private UsersBean assiger;
	
	private UsersBean assignee;
	private PermissionAccessType status; // denied
	
	private String description;
	
	private long priorityLevel = 0;
	
//	private List<RolesBean> userRoles;
	private String level;
//	
	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(generator="permission_gen_seq", strategy=GenerationType.SEQUENCE)
	@UnBound
	public Integer getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	
	/**
	 * @return the actionID
	 */
	@UnBound
	@UiComponentPosition(positionColumn = "1", positionRow = "1", tableDisplayColumn= "-1")
	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity=ActionBean.class)
	@JoinTable(name = "eagle_perm_action", joinColumns={@JoinColumn(name = "perm_id", referencedColumnName= "id")},
			inverseJoinColumns={@JoinColumn(name = "action_id", referencedColumnName = "id")})
	public List<ActionBean> getActionID() {
		return actionID;
	}

	/**
	 * @param actionID the actionID to set
	 */
	public void setActionID(List<ActionBean> actionID) {
		this.actionID = actionID;
	}

	/**
	 * @return the permissionType
	 */
	@UiComponentPosition(positionColumn = "1", positionRow = "2", tableDisplayColumn= "-1")
	@Column(name = "perm_type")
	@Enumerated(EnumType.STRING)
	public PermissionType getPermissionType() {
		return permissionType;
	}

	/**
	 * @param permissionType the permissionType to set
	 */
	public void setPermissionType(PermissionType permissionType) {
		this.permissionType = permissionType;
	}

	/**
	 * @return the assiger
	 */
	@UnBound
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "assignerID", referencedColumnName = "id")
	public UsersBean getAssiger() {
		return assiger;
	}

	/**
	 * @param assiger the assiger to set
	 */
	public void setAssiger(UsersBean assiger) {
		this.assiger = assiger;
	}


	/**
	 * @return the assignee
	 */
	@UnBound
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "assigneeID", referencedColumnName = "id")
	public UsersBean getAssignee() {
		return assignee;
	}

	/**
	 * @param assignee the assignee to set
	 */
	public void setAssignee(UsersBean assignee) {
		this.assignee = assignee;
	}

	/**
	 * @return the status
	 */
	@UiComponentPosition(positionColumn = "2", positionRow = "2", tableDisplayColumn= "1")
	@Column(name = "status")
	@Enumerated(EnumType.STRING)
	public PermissionAccessType getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(PermissionAccessType status) {
		this.status = status;
	}

	/**
	 * @return the description
	 */
	@UiComponentPosition(positionColumn = "1", positionRow = "3", tableDisplayColumn= "1")
	@Column(name = "description", length = 100)
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}


	/**
	 * @return the priorityLevel
	 */
	@UiComponentPosition(positionColumn = "2", positionRow = "3", tableDisplayColumn= "1")
	@PropertyConstraints(isMandatory = MandatoryType.TURE, maxLength = "10", inputCaseType = StringInputCaseType.UPPERCASE, textInputDataType = TextFiledInputDataType.ONLYNUMBER)
	@Column(name = "periorityLevel")
	public long getPriorityLevel() {
		return priorityLevel;
	}

	/**
	 * @param priorityLevel the priorityLevel to set
	 */
	public void setPriorityLevel(long priorityLevel) {
		this.priorityLevel = priorityLevel;
	}

//	/**
//	 * @return the userRoles
//	 */
//	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//	public List<RolesBean> getUserRoles() {
//		return userRoles;
//	}
//
//	/**
//	 * @param userRoles the userRoles to set
//	 */
//	public void setUserRoles(List<RolesBean> userRoles) {
//		this.userRoles = userRoles;
//	}

	/**
	 * @return the level
	 */
	@UiComponentPosition(positionColumn = "1", positionRow = "4", tableDisplayColumn= "1")
	@PropertyConstraints(isMandatory = MandatoryType.TURE, maxLength = "8", textInputDataType = TextFiledInputDataType.ONLYALPHA)
	@Column(name = "role_levelID")
	public String getLevel() {
		return level;
	}

	/**
	 * @param level the level to set
	 */
	public void setLevel(String level) {
		this.level = level;
	}
}